import config from "../../config";
import water from "../canvas/water";
import wall from "../canvas/wall";
import steel from "../canvas/steels";
import boss from "../canvas/boss";

export default {
  // 四周边缘碰撞
  isCanvasTouch(x: number, y: number, width = config.model.width, height = config.model.height) {
    return x < 0 || x + width > config.canvas.width || y < 0 || y + height > config.canvas.height;
  },
  // 模型触碰
  isModelTouch(
      x: number,
      y: number,
      width = config.model.width,
      height = config.model.height,
      models = [...wall.models, ...steel.models, ...water.models, ...boss.models]
  ): IModel | undefined {
    if (width < 30) {
      x = x - config.model.width / 2 + config.bullet.width / 2 // x = 11 - 15 + 4
      y = y - config.model.height / 2 + config.bullet.height / 2
      width = config.model.width - 7
      height = config.model.height - 7
    }
    return models.find(model => {
      // 都不满足这些条件说明碰撞了, state为false 返回!state为true
      const state =
          x + width <= model.position.x ||
          x >= width + model.position.x ||
          y + height <= model.position.y ||
          y >= height + model.position.y
      return !state
    })
  }
}
